<%@page import="com.neiquan.backstage.pub.bsc.dao.po.User"%>
<%@page import="com.neiquan.backstage.common.util.Sessions"%>
<%@page import="com.neiquan.backstage.pub.bsc.dao.po.MeetingRoom"%>
<%@page import="com.neiquan.backstage.meeting_room.MeetingRoomService"%>
<%@page import="com.neiquan.backstage.common.util.Configs"%>
<%@page import="java.util.List"%>
<%@page import="com.neiquan.backstage.common.util.SpringContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
User user=(User)Sessions.getLoginedUser(session);
Integer memberId=user.getMemberId();
ApplicationContext appCtx = SpringContextUtils.getApplicationContext();
MeetingRoomService meetingRoomService = appCtx.getBean(MeetingRoomService.class);
List<MeetingRoom> listM = meetingRoomService.queryByMemberId(memberId);
pageContext.setAttribute("listM", listM);
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta charset="UTF-8">
	<title>会议室预定</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1.0" />
	<meta name="apple-mobile-web-app-capable" content="yes" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<link rel="stylesheet" href="css/base.css" />
	<link rel="stylesheet" href="css/main.css" />
	<style type="text/css">
	.leaseimg2 {
	    background-color: #808080;
	}
	</style>
</head>
<body bgcolor="#f0f0f0" id="app" ng-app="app" ng-controller="ctrl">
		<section class="common_bg addr_wrap">
		<form method="post">
			<section class="relative tc area-wrap">
				会议室<input readonly="readonly" type="text" placeholder="请选择会议室"  name="meetingRoomName" class="addr_inpt inpt" />
				<ul class="area_select boardroom-select" style="display: none">
				<%
					if(listM.size()>0){
					for(MeetingRoom m:listM){ %>
						<li ng-click="schedule(<%=m.getMeetingRoomId()%>)"><%=m.getName() %></li>
					<%} 
					}%>
				</ul>
			</section>
			<section class="line addr_line"></section>
			<section class="mt">
				<table id="schedule1" class="addr_table boardroom-table tc" cellpadding="0" cellspacing="0">
				
				</table>
				<p class="tc">
					<span class="diamond">您预定的时间</span>
					<span class="diamond diamond_right">已经被锁定</span>
				</p>
			</section>
			<section class="addr_sub pd">
				<input type="button" class="btn rent_submmit" value="确定" ng-click="submit()">
			</section>
			</form>
		</section>
</body>
<script src="js/jquery.min.js"></script>
<script src="angular-1.5.0-rc.2/angular.js"></script>
<script src="js/main.js"></script>
<script src="js/responsive.js"></script>
<script>
	$(function(){
		var zhubajieH = jQuery('.copyprint-top-right').height();
		jQuery('.copyprint-top').height(zhubajieH);
		jQuery('.copyprint-bottom').height(window.innerHeight - zhubajieH - 60);
	})
</script>
<script >
		var app = angular.module("app", []);
		app.controller('ctrl',function($scope, $http, $httpParamSerializer,$injector, $window) {
				$scope.submit = function() {
					var items=document.getElementsByName("meetingTimes");
					var arr=new Array();
					var date1=new Date();
					var i=0;
						for(var x=0;x<items.length;x++){
							if(items[x].checked){
								date1= items[x].value;
								 arr[i] = date1;
								 i++;
								}
							}
						arr[i] = "";
					if(arr.length==0||arr.length<0){
						alert("至少选一个");
					}else{
					var param = {
							"meetingRoomId":meetingRoomId,
							"orderTimes":arr
							
					};
				jQuery.post("api/meetingOrderAction/add",param, function(data) {
							alert("预约成功，等待审核");
							$window.location.href="myworkaddr.jsp";
									});
					}
				}
				
		//生成时间表
		var meetingRoomId=0;
	 	$scope.schedule=function(_meetingRoomId){
	 		meetingRoomId = _meetingRoomId;
	 		var reqDate = jQuery.param({
	 		    "meetingRoomId": meetingRoomId
	 		});
	 		var reqConfig = {
	 		    headers: {
	 		        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
	 		    }
	 		};
	 		$http.post("api/meetingOrderAction/findByAddressAll", reqDate, reqConfig).success(function(data, status) {
	 		    if (data.code != 0) {
	 		    	alert(data.errorMessage);
	 		        return;
	 		    }
	 		    $scope.meetings = data.response;
	 		   var weekLabels = new Array('日', '一', '二', '三', '四', '五', '六');
				var currentTime = new Date();
		 		jQuery('#schedule1').empty();
		 		// 表头占两行
	            jQuery('#schedule1').append("<tr id='tr0'></tr>");
	            jQuery('#schedule1').append("<tr id='tr1'></tr>");
	            for (var j = 0; j < 8; j++) {
	               if (j == 0) {
	                   jQuery("#tr0").append("<td >" + (currentTime.getMonth() + 1) + "月</td>");
	                   jQuery("#tr1").append("<td>星期</td>");
	               } else {
	                   var offset = j == 1 ? 0 : 1;
	                   currentTime.setDate(currentTime.getDate() + offset);
	                   jQuery("#tr0").append("<td >" + currentTime.getDate() + "日</td>");
	                   jQuery("#tr1").append("<td   >" + weekLabels[currentTime.getDay()] + "</td>");
	               }
	            }
	            // 剩余的表体
	            for (var i = 2; i < 15; i++) {
			        jQuery('#schedule1').append("<tr id='tr" + i + "'></tr>");
			        currentTime = new Date();
	            	for (var j = 0; j < 8; j++) {
	                    if (j == 0) {
	                    	jQuery("#tr" + i).append("<td >" + (i + 7) + "-" + (i + 8) + "</td>");
	                    } else {
	                    	var used = false;
	                        var offset = j == 1 ? 0 : 1;
	                        currentTime.setDate(currentTime.getDate() + offset);
	                        var year = currentTime.getFullYear();
	                        var month = currentTime.getMonth() + 1;
	                        var day = currentTime.getDate();
	                        var hours = i + 7;
	                        for (var f = 0; f < $scope.meetings.length; f++) {
	                            var times = $scope.meetings[f].times.split(',');
	                            for (y = 0; y < times.length; y++) {
	                                var time = times[y].split('-');
	                                var _year = time[0];
	                                var _month = time[1];
	                                var _day = time[2];
	                                var _hours = time[3];
	                                if (year == _year && month == _month && day == _day && hours == _hours) {
	                                	used = true;
	                                    break;
	                                }
	                            }
	                        }
	                        if (used) {
	                        	jQuery("#tr" + i).append("<td class='leaseimg2'> </td>");
	                        } else {
		                        var time = currentTime.getFullYear()+'-'+(currentTime.getMonth()+1)+'-'+currentTime.getDate();
		                        jQuery("#tr" + i).append("<td id=td" + i + j + " onclick='clickChange(" + i + j + ")'>" + "<input type='checkbox' id=meeting" + i + j + " name='meetingTimes' value='" + time + "," + (i + 7) + "' style='display:none;'>" + "<input type='hidden' id='sequence" + i + j + "' value='" + currentTime.getDate() + "," + i + "'>" + "<input id=status" + i + j + " value='0' style='display:none;'>" + " </td>");
	                        }
	                    }
	                 }
	            }
	 		}).error(function(data, status) {
	 			alert(status);
	 		});
		}
		});
		var a=0;
		var b=0;
		var f=0;
		var h=0;
		//初始化数据
		function initNumber(){
			a=0;
			b=0;
			f=0;
			h=0;
		}
		function clickChange(value){
			var stat=document.getElementById("status"+value).value;
			if(stat==0){
				var sequence=document.getElementById("sequence"+value).value;
				var str=sequence.split(",");
				if((a==null&&b==null)||(a==0&&b==0)){
					a=str[0];
					b=str[1];
					f=str[1];
					h=str[1];
					document.getElementById("td"+value).style.background= "#DC143C";
					var stationStatus=document.getElementById("meeting"+value);
					stationStatus.checked=true;
					document.getElementById("status"+value).value=1;
				}else{
					var c=str[0];
					var d=str[1];
					if(a==c){
						var e=parseInt(d)-1;
						var g=parseInt(d)+1;
						if(e==b||g==h){
							document.getElementById("td"+value).style.background= "#DC143C";
							var stationStatus=document.getElementById("meeting"+value);
							stationStatus.checked=true;
							document.getElementById("status"+value).value=1;
							if(h==g){
								h=d;
								f=d;
							}else{
								b=d;
							}
						}else{
							alert("请选择连续时间");
							return;
						}
					}else{
						alert("一次只能预约一天，如果要预约多天请多次下订单");
						return;
					}
				}
			}else{
				var sequence=document.getElementById("sequence"+value).value;
				var str=sequence.split(",");
				var c=str[0];
				var d=str[1];
				if(c==a){
					if(b==d){
					document.getElementById("td"+value).style.background= "#ffffff";
					var stationStatus=document.getElementById("meeting"+value);
					stationStatus.checked=false;
					document.getElementById("status"+value).value=0;
					if(d==f){
						a=0;
						b=0;
						f=0;
					}else{
						b=parseInt(d)-1
						}
					}else{
						alert("请从最后一个时间段递减");
						return;
					}
				}else{
					alert("请从最后一个时间段递减");
					return;
				}
			}
		};
</script>
</html>
